/*
* @author : lj
* @creation : 2014-6-10 下午3:20:00
* @description : 
*	我的停车场管理，用户：停车场管理员，可以添加停车场和维护停车场信息
*/

package com.action.park;

import java.sql.Timestamp;
import java.util.List;
import com.bean.Parkinfo;
import com.bean.ParkinfoManagerRelevance;
import com.service.ParkinfoService;
import com.util.GUtil;

public class ParkAreaAction extends BaseParkAction {

	ParkinfoService service;
	Parkinfo parkInfo;
	List<Parkinfo> list=null;
	
	/**
	 * <p>Title: handle 默认处理方法</p>
	 * <p>Description:  默认处理方法</p>
	 * @return see com.util.Util#
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#handle()
	 */
	@Override
	public String handle() throws Exception {
		if(id!=null && id>0)
			return this.view();
		return this.query();
	}
	/**
	 * <p>Title: query 查询我的停车场信息</p>
	 * <p>Description: 查询我的停车场信息</p>
	 * @return 我的停车场列表
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		list = service.query(parkInfo, page);// service.getMyParkArea(parkuser);
		return GUtil.SUCCESS;
	}

	@Override
	public String add() throws Exception {
		this.setHeaderTitle("添加");
		if(parkInfo==null){
			return GUtil.EDIT;
		}
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		parkInfo.setCreateTime(ts);
		parkInfo.setUpdateTime(ts);
		ParkinfoManagerRelevance pmr = new ParkinfoManagerRelevance();
		pmr.setUserId( user.getId());
		if(service.add(parkInfo, pmr).endsWith(GUtil.SUCCESS)){
			this.setMsg("添加成功");
			parkInfo = null;
		}else{
			this.setMsg("添加失败");
		}
		return GUtil.EDIT;
	}

	@Override
	public String update() throws Exception {
		this.setHeaderTitle("修改");
		if(id==null){
			this.setMsg("修改停车场信息异常");
			return GUtil.ERROR;
		}else if(parkInfo==null){//beforeUpdate
			parkInfo = (Parkinfo) service.get(id.intValue());
			if(parkInfo==null){
				this.setMsg("信息不存在 id="+id);
				return GUtil.ERROR;
			}
			if(parkInfo.getStatus()!=3 && parkInfo.getStatus()!=-2){
				this.setMsg("此条信息不可更改["+parkInfo.getStatusName()+"]");
				return this.query();
			}
			return GUtil.EDIT;
		}
		//update
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		parkInfo.setUpdateTime(ts);
		parkInfo.setPId(id.intValue());
		
		if(service.update(parkInfo).endsWith(GUtil.SUCCESS)){
			this.setMsg("修改成功");
			parkInfo = null;
			return this.query();
		}else{
			this.setMsg("修改失败");
			return GUtil.EDIT;
		}
	}
	
	@Override
	public String delete() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
		else
			return service.delete( id.intValue(), 0);
	}

	@Override
	public String view() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
		parkInfo = (Parkinfo) service.get( id.intValue());
		return GUtil.VIEW;
	}

	public ParkinfoService getService() {
		return service;
	}

	public void setService(ParkinfoService service) {
		this.service = service;
	}

	public Parkinfo getParkInfo() {
		return parkInfo;
	}

	public void setParkInfo(Parkinfo parkInfo) {
		this.parkInfo = parkInfo;
	}

	public List<Parkinfo> getList() {
		return list;
	}

	public void setList(List<Parkinfo> list) {
		this.list = list;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}
}
